package org.zasadnyy.redlions.players.server.converters.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zasadnyy.redlions.players.server.converters.TournamentsConverter;
import org.zasadnyy.redlions.players.server.dao.Dao;
import org.zasadnyy.redlions.players.server.dao.impl.JdoTournmaentDao;
import org.zasadnyy.redlions.players.server.model.TournamentObject;
import org.zasadnyy.redlions.players.shared.model.TournamentItem;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class TournamentConverterImpl implements TournamentsConverter {

	private static final Logger LOG = LoggerFactory
			.getLogger(TournamentConverterImpl.class);

	private Dao<TournamentObject> dao = new JdoTournmaentDao();

	@Override
	public TournamentObject toObject(TournamentItem item) {
		// LOG.info("toObject called");
		if(item == null){
			throw new IllegalArgumentException("item == null");
		}
		Key objectKey = null;
		if (item.getId() != null) {
			objectKey = KeyFactory.stringToKey(item.getId());
		}
		Set<Key> playerIds = new HashSet<Key>();
		for (String stringId : item.getPlayerIds()) {
			playerIds.add(KeyFactory.stringToKey(stringId));
		}
		TournamentObject object = new TournamentObject(objectKey,
				item.getTitle(), item.getUrl(), item.getTime(), playerIds);
		return object;
	}

	@Override
	public TournamentItem toItem(TournamentObject object) {
		// LOG.info("toItem called");
		String itemKey = KeyFactory.keyToString(object.getId());
		List<String> stringKeys = new ArrayList<String>();
		for (Key playerKey : object.getPlayers()) {
			stringKeys.add(KeyFactory.keyToString(playerKey));
		}
		TournamentItem item = new TournamentItem(itemKey, object.getTitle(),
				object.getUrl(), object.getTime(), stringKeys);
		return item;
	}

	@SuppressWarnings("deprecation")
	@Override
	public Map<Integer, List<TournamentItem>> toItemList(Set<Key> tournamentKeys) {
		// LOG.info("toItemList called");
		Map<Integer, List<TournamentItem>> groupedTournaments = new HashMap<Integer, List<TournamentItem>>();
		if (tournamentKeys != null) {
			for (Key tournamentKey : tournamentKeys) {
				TournamentObject object = dao.getById(tournamentKey);
				TournamentItem item = toItem(object);
				int year = item.getTime().getYear() + 1900;
				if (groupedTournaments.keySet().contains(year)) {
					groupedTournaments.get(year).add(item);
				} else {
					List<TournamentItem> itemsForYear = new ArrayList<TournamentItem>();
					itemsForYear.add(item);
					groupedTournaments.put(year, itemsForYear);
				}
			}
		} else {
			LOG.warn("Set<Key> tournaments == NULL");
		}

		return groupedTournaments;
	}

	@Override
	public Set<Key> toObjectList(Map<Integer, List<TournamentItem>> tournaments) {
		// LOG.info("toObjectList called");
		Set<Key> tournamentKeys = new HashSet<Key>();
		if (tournaments != null) {
			for (Map.Entry<Integer, List<TournamentItem>> entry : tournaments
					.entrySet()) {
				for (TournamentItem item : entry.getValue()) {
					TournamentObject tournamentObject = toObject(item);
					tournamentKeys.add(tournamentObject.getId()); // TODO: fix
																	// problem
																	// with new
																	// tournaments
				}
			}
		} else {
			LOG.warn("Map<Integer, List<TournamentItem>> tournaments == NULL");
		}
		return tournamentKeys;
	}

	@Override
	public List<TournamentItem> toItemList(
			List<TournamentObject> objects) {
		List<TournamentItem> itemList = new ArrayList<TournamentItem>();
		if (objects != null) {
			for (TournamentObject object : objects) {
				TournamentItem item = toItem(object);
				itemList.add(item);
			}
		} else {
			LOG.warn("Set<Key> tournaments == NULL");
		}

		return itemList;
	}

}
